package cn.iocoder.yudao.module.reading.controller.app.test;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.reading.controller.app.bookset.vo.BookSetRespVO;
import cn.iocoder.yudao.module.reading.framework.util.StaticResourceUrlUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * URL转换测试控制器
 * 用于测试静态资源URL转换功能
 * 
 * @author 李晨
 */
@Tag(name = "用户 APP - URL转换测试")
@RestController
@RequestMapping("/reading/test/url")
@Slf4j
public class UrlTestController {

    @Resource
    private StaticResourceUrlUtils staticResourceUrlUtils;

    @GetMapping("/convert")
    @Operation(summary = "测试URL转换功能")
    public CommonResult<Map<String, Object>> testUrlConvert(@RequestParam String relativePath) {
        log.info("测试URL转换，输入相对路径: {}", relativePath);
        
        Map<String, Object> result = new HashMap<>();
        
        // 测试相对路径转完整URL
        String fullUrl = staticResourceUrlUtils.toFullUrl(relativePath);
        result.put("relativePath", relativePath);
        result.put("fullUrl", fullUrl);
        
        // 测试完整URL转相对路径
        String backToRelative = staticResourceUrlUtils.toRelativePath(fullUrl);
        result.put("backToRelative", backToRelative);
        
        // 获取配置信息
        result.put("minioDomain", staticResourceUrlUtils.getMinioDomain());
        result.put("bucketName", staticResourceUrlUtils.getBucketName());
        
        log.info("URL转换结果: {}", result);
        
        return success(result);
    }

    @GetMapping("/test-vo")
    @Operation(summary = "测试VO对象URL转换")
    public CommonResult<BookSetRespVO> testVoUrlConvert() {
        log.info("测试VO对象URL转换");
        
        // 创建一个测试的BookSetRespVO对象
        BookSetRespVO bookSet = new BookSetRespVO();
        bookSet.setId(1L);
        bookSet.setTitle("测试绘本集");
        bookSet.setCover("/reading-app-files/book-sets/covers/1/test-cover.jpg");
        bookSet.setDescription("这是一个测试绘本集");
        bookSet.setBookCount(5);
        bookSet.setLanguage("chinese");
        bookSet.setIsRecommended(true);
        
        log.info("转换前的封面URL: {}", bookSet.getCover());
        
        // 调用URL转换
        bookSet.convertUrls();
        
        log.info("转换后的封面URL: {}", bookSet.getCover());
        
        return success(bookSet);
    }

    @GetMapping("/config")
    @Operation(summary = "获取当前URL配置信息")
    public CommonResult<Map<String, Object>> getUrlConfig() {
        Map<String, Object> config = new HashMap<>();
        config.put("minioDomain", staticResourceUrlUtils.getMinioDomain());
        config.put("bucketName", staticResourceUrlUtils.getBucketName());
        
        // 检查系统属性
        String returnRelativePath = System.getProperty("yudao.file.return-relative-path", "false");
        config.put("returnRelativePath", returnRelativePath);
        
        return success(config);
    }
}
